package com.pwc.conversionTool.rest;

import java.util.HashMap;

import org.dom4j.DocumentException;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.pwc.conversionTool.soapUI.Client;
import com.pwc.conversionTool.xml.XMLFactory;
import com.pwc.conversionTool.xml.XMLNode;

@Controller
public class SoapRest {

	Client client = null;

	@RequestMapping(value = "/rest/operationMap", method = RequestMethod.GET, produces = "application/json")
	public @ResponseBody HashMap<String, String> initial(String url) {

		client = new Client();

		client.InitialProject(url);

		HashMap<String, String> map = (HashMap<String, String>) client
				.GetOperationMap();
		return map;
	}

	@RequestMapping(value = "/rest/XMLNode", method = RequestMethod.GET, produces = "application/json")
	public @ResponseBody XMLNode getSimpleXMLJson() throws DocumentException
	{
		client = new Client();
		
		client.InitialProject("https://wd2-impl-services1.workday.com/ccx/service/pwc_dpt1/Staffing/v25.0?wsdl");
	
		HashMap<String, String> map = (HashMap<String, String>) client
				.GetOperationMap();
		String xmlString=map.get("Put_Applicant|Request 1");
		
		XMLFactory factory=new XMLFactory(xmlString);
		
		return factory.getRootNode();
	}

	@RequestMapping(value = "/rest/sendSoapSecurity", method = RequestMethod.GET, produces = "application/json")
	public @ResponseBody HashMap<String, String> sendSoapSecurity(String data,
			String operationName, String requestName, String userName,
			String password, String passwordType) {

		String Response = "";

		HashMap<String, String> resultHashMap = new HashMap<String, String>();

		try {
			client.GenerateWSDLRequest(operationName, requestName, data);

			client.SetSecurity(userName, password, passwordType);

			Response = client.DoSubmit();

			System.out.println(Response);

			resultHashMap.put("Response", Response);
		} catch (Exception e) {
			e.printStackTrace();
		}

		return resultHashMap;
	}

	@RequestMapping(value = "/rest/sendSoap", method = RequestMethod.GET, produces = "application/json")
	public @ResponseBody HashMap<String, String> sendSoap(String data,
			String operationName, String requestName) {

		String Response = "";

		HashMap<String, String> resultHashMap = new HashMap<String, String>();

		try {
			client.GenerateWSDLRequest(operationName, requestName, data);

			Response = client.DoSubmit();

			System.out.println(Response);

			resultHashMap.put("Response", Response);
		} catch (Exception e) {
			e.printStackTrace();
		}

		return resultHashMap;
	}

}